package com.zhiling.www.service;

import com.zhiling.www.entity.*;
import com.zhiling.www.mapper.EntrustOrderMapper;
import com.zhiling.www.mapper.StockMapper;
import com.zhiling.www.mapper.WarehouseMapper;
import com.zhiling.www.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private EntrustOrderMapper entrustOrderMapper;
    @Autowired
    private StockMapper stockMapper;

    //查询当前用户持仓信息
    @Override
    public Result list(String token) {
        Result result = new Result();
        //解析token，获取用户id
        Claims claims = JwtUtils.parseToken(token);
        int userId = Integer.parseInt(String.valueOf(claims.get("userId")));
        //查询当前用户持仓信息
        Example example = new Example(Warehouse.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wareUserid",userId);
        List <Warehouse> warehouses = warehouseMapper.selectByExample(example);
        //加载每一种股票的可卖数量
        for (Warehouse warehouse:warehouses) {
            Example example2 = new Example(EntrustOrder.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("oStockUid",userId);
            criteria2.andEqualTo("oStockType",0);
            criteria2.andEqualTo("oStockCode",warehouse.getWareStockcode());
            criteria2.andEqualTo("oStatus",1);
            //统计今天成交的股票数量
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            format = format + " 00:00:00";
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time = null;
            try {
                time = sdf2.parse(format);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria2.andGreaterThan("oEntrusttime",time);
            List <EntrustOrder> entrustOrders = entrustOrderMapper.selectByExample(example2);
            //今天成交的股票数量
            int num = 0;
            for (EntrustOrder entrustOrder :entrustOrders) {
                num += entrustOrder.getoStockNum();
            }
            warehouse.setWareSellnum(warehouse.getWareStocknum() - num);
        }
        result.setCode(200);
        result.setData(warehouses);
        return result;
    }

    //加载每一种股票的可卖数量
    @Override
    public Result loadSellNum(String stockcode,String token) {
        Result result = new Result();
        //解析token，获取用户id
        Claims claims = JwtUtils.parseToken(token);
        int userId = Integer.parseInt(String.valueOf(claims.get("userId")));
        Example example = new Example(EntrustOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("oStockUid",userId);
        criteria.andEqualTo("oStockType",0);
        criteria.andEqualTo("oStockCode",stockcode);
        criteria.andEqualTo("oStatus",1);
        //统计今天成交的股票数量
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        format = format + " 00:00:00";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = null;
        try {
            time = sdf2.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        criteria.andGreaterThan("oEntrusttime",time);
        List <EntrustOrder> entrustOrders = entrustOrderMapper.selectByExample(example);
        //今天成交的股票数量
        int num = 0;
        for (EntrustOrder entrustOrder :entrustOrders) {
            num += entrustOrder.getoStockNum();
        }
        result.setCode(200);
        result.setData(num);
        return result;
    }

    //查询当前用户单个股票持仓信息
    @Override
    public Result loadStock(String codeOrName, String token) {
        Result result = new Result();
        //解析token，获取用户id
        Claims claims = JwtUtils.parseToken(token);
        int userId = Integer.parseInt(String.valueOf(claims.get("userId")));
        //查询股票代码
        Example example2 = new Example(Stock.class);
        Example.Criteria criteria2 = example2.createCriteria();
        //根据股票代码或者股票名称查找
        criteria2.orEqualTo("stockCode",codeOrName);
        criteria2.orEqualTo("stockName",codeOrName);
        Stock stock = stockMapper.selectOneByExample(example2);
        if (stock == null){
            result.setCode(400);
            result.setMsg("没有找到该股票");
            return result;
        }
        //查询该股票总市值
        Example example = new Example(Warehouse.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wareUserid",userId);
        criteria.andEqualTo("wareStockcode",stock.getStockCode());
        Warehouse warehouse = warehouseMapper.selectOneByExample(example);
        if (warehouse == null){
            result.setCode(400);
            result.setMsg("您的持仓中没有该股票");
            return result;
        }
        //市值
        double marketPrice = warehouse.getWareStocknum() * 100 * warehouse.getWareStockprice();
        //查询该股票的可卖数量
        Example example3 = new Example(EntrustOrder.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("oStockUid",userId);
        criteria3.andEqualTo("oStockType",0);
        criteria3.andEqualTo("oStockCode",stock.getStockCode());
        criteria3.andEqualTo("oStatus",1);
        //统计今天成交的股票数量
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        format = format + " 00:00:00";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = null;
        try {
            time = sdf2.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        criteria3.andGreaterThan("oEntrusttime",time);
        List <EntrustOrder> entrustOrders = entrustOrderMapper.selectByExample(example3);
        //今天成交的股票数量
        int num = 0;
        for (EntrustOrder entrustOrder :entrustOrders) {
            num += entrustOrder.getoStockNum();
        }
        Map<String,Object> map = new HashMap <>();
        map.put("market",marketPrice);
        map.put("canSellnum",warehouse.getWareStocknum() - num);
        result.setCode(200);
        result.setData(map);
        return result;
    }
}
